﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace YetAnotherChat.TcpServer.WebSocketProtocols
{
    /// <summary>
    /// Base class for authentication
    /// </summary>
    public abstract class AuthenticationBase
    {
        private static string _origin = string.Empty;
        private static string _destination = string.Empty;

        public static string Origin
        {
            get { return _origin; }
            set { _origin = value; }
        }

        public static string Destination
        {
            get { return _destination; }
            set { _destination = value; }
        }


        /// <summary>
        /// Attempts to authenticates the specified user context.
        /// If authentication fails it kills the connection.
        /// </summary>
        /// <param name="connection">The user context.</param>
        public bool Authenticate(ServerConnection connection)
        {
            if (CheckAuthentication(connection))
            {
                connection.RequestPath = connection.Header.RequestPath;
                connection.Header = null;
                connection.IsSetup = true;
                connection.Server.OnClientConnected(new ConnectionEventArgs(connection));
                return true;
            }
            else
            {
                connection.Disconnect();
                return false;
            }
        }


        protected abstract bool CheckAuthentication(ServerConnection connection);
    }
}
